/*
 * Copyright 2012-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.j360.springboot.simple.service.activemq;

import me.j360.springboot.simple.bean.DestinationConstant;
import me.j360.springboot.simple.dao.AdminDao;
import me.j360.springboot.simple.entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.jms.Destination;
import javax.jms.Queue;
import javax.transaction.Transactional;
import java.util.Map;

@Transactional
@Component
public class JmsProducer {

	@Autowired
	private JmsMessagingTemplate jmsMessagingTemplate;

	@Autowired
	private Queue queue;
    @Autowired
    private AdminDao adminDao;

	public void send(String msg) {

        this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
	}

    public void send(String destinationName,Object payLoad,Map<String,Object> headers){
        this.jmsMessagingTemplate.convertAndSend(destinationName,payLoad,headers);
    }


    public void sendSms(){
        Admin admin = new Admin();
        admin.setIsAccountEnabled(true);
        admin.setIsAccountExpired(true);
        admin.setIsCredentialsExpired(false);
        admin.setIsAccountLocked(true);
        admin.setUsername("aaa");
        admin.setPassword("2234");
        admin.setSalt("adfadf");

        adminDao.save(admin);
        Sms sms = new Sms();
        sms.setId(admin.getId());
        sms.setText("nihao");

        this.send(DestinationConstant.CSV_IMPORT_MESSAGE, sms, null);

        System.out.println(admin.getUsername());
    }

}
